package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理控制层
 *
 * @Author mr.zhang
 * Date 2025/5/21 15:57
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     */
    @PostMapping
    @ApiOperation("新增套餐")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}",setmealDTO);
        try {
            setmealService.add(setmealDTO);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页条件查询套餐
     */
    @GetMapping("/page")
    @ApiOperation("分页条件查询套餐")
    public Result<PageResult> listPage(SetmealPageQueryDTO dto){
        log.info("分页条件查询套餐",dto);
        PageResult pageResult = setmealService.listPage(dto);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除套餐",ids);
        try {
            setmealService.delete(ids);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    /**
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result getById(@PathVariable("id") Long id){
        SetmealVO setmealVO = null;
        try {
            setmealVO = setmealService.getById(id);
            return Result.success(setmealVO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐", dto);
        setmealService.update(dto);
        return Result.success();
    }


    /**
     * 修改套餐起售与停售状态
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐起售与停售状态")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        try {
            setmealService.updateStatus(status,id);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }
}
